using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using BaseData.ApplicationLib.Commands;
using BaseData.ApplicationLib.Queries;
using BaseData.CommonLib.Constants;
using BaseData.CommonLib.Models;
using MediatR;
using Microsoft.AspNetCore.Mvc;

namespace BaseData.WebApi.Controllers
{
    [ApiController]
    public class SupplierController : ControllerBase
    {
        private readonly IMediator _mediator;
        private readonly ISupplierQueries _supplierQueries;

        public SupplierController(IMediator mediator,
            ISupplierQueries unitQueries)
        {
            _mediator = mediator;
            _supplierQueries = unitQueries;
        }

        [HttpGet(ApiRoutes.Supplier.QuerySuppliers)]
        public async Task<IActionResult> Get([FromQuery] SupplierSearchRequestDto model)
        {
            if (model.Page > 0 || model.Limit > 0)
            {
                var result = await _supplierQueries.QuerySuppliersByPage(model);
                return Ok(result);
            }
            else
            {
                var result = await _supplierQueries.QuerySuppliers(model);
                return Ok(result);
            }
        }

        [HttpGet(ApiRoutes.Supplier.GetSupplier)]
        public async Task<IActionResult> GetSupplier(int id)
        {
            var result = await _supplierQueries.GetSupplier(id);
            return Ok(result);
        }

        [HttpPost(ApiRoutes.Supplier.CreateSupplier)]
        public async Task<IActionResult> Post(CreateSupplierCommand command)
        {
            var result = await _mediator.Send(command);
            return Ok(result);
        }

        [HttpPut(ApiRoutes.Supplier.UpdateSupplier)]
        public async Task<IActionResult> Put(int id, [FromBody] UpdateSupplierCommand command)
        {
            command.Id = id;
            var result = await _mediator.Send(command);
            return Ok(result);
        }
    }
}